package com.huicoo.forestmanager.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;

import com.huicoo.forestmanager.R;
import com.huicoo.forestmanager.util.LogUtils;

import java.util.Timer;
import java.util.TimerTask;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/10		    Administrator
 * 进行巡查任务的前台服务
 */
public class ForegroundService extends Service {
    private static final String CHANNEL_ID = "ForestCampTask";
    private static final int NOTIFY_ID = 1339;
    private boolean isPlay = false;

    private int second; //任务进行计时
    /**
     * 回调
     */
    private Callback callback;
    /**
     * Timer实时更新数据的
     */
    private Timer mTimer = new Timer();

    private PowerManager.WakeLock wakeLock;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public ForegroundService getService() {
            return ForegroundService.this;
        }

        public void start() {
            play();
        }

        public void stop() {
            stopPlay();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    private void play() {
        if (!isPlay) {
            isPlay = true;

            //尝试让系统不休眠
            if (wakeLock == null) {
                PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
                wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ForestManager:ForegroundService");
            }
            if (wakeLock != null && !wakeLock.isHeld()) {
                wakeLock.acquire();
                LogUtils.v("gogogo wakelock acquire");
            }

            Notification myNotify;
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
                myNotify = new Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setTicker("林长通")
                        .setContentTitle("林长通")
                        .setContentText("巡查任务进行中")
                        .build();
            } else {
                //适配8.0service
                NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
                NotificationChannel mChannel;
                mChannel = new NotificationChannel(CHANNEL_ID, "林长通", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
                myNotify = new Notification.Builder(getApplicationContext(), CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setTicker("林长通")
                        .setContentTitle("林长通")
                        .setContentText("巡查任务进行中")
                        .build();
            }

            myNotify.flags |= Notification.FLAG_NO_CLEAR;
            startForeground(NOTIFY_ID, myNotify);

            startService(new Intent(this, SingASongService.class));

            if (mTimer != null) {
                mTimer.schedule(task, 0, 1000);
            }
        }
    }

    private void stopPlay() {
        if (isPlay) {
            isPlay = false;

            if (wakeLock != null && wakeLock.isHeld()) {
                wakeLock.release();
                LogUtils.v("gogogo wakelock release");
            }

            stopForeground(true);

            stopService(new Intent(this, SingASongService.class));

            if (mTimer != null) {
                mTimer.cancel();
            }
        }
    }

    @Override
    public void onDestroy() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        super.onDestroy();
    }

    /**
     * 提供接口回调方法
     */
    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    /**
     * 定时器线程
     */
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            second++;
            if (callback != null) {
                /*
                 * 得到最新数据
                 */
                callback.resetTime(second);
            }

        }

    };


    /**
     * 回调接口
     */
    public interface Callback {

        void resetTime(int second);
    }
}
